package org.javaopenaccess.web.ui.registration;

import java.util.logging.Logger;

import org.javaopenaccess.forms.UserForm;
import org.javaopenaccess.service.user.UserService;
import org.javaopenaccess.web.ui.classes.NewClassController;
import org.zkoss.util.logging.Log;
import org.zkoss.zk.ui.Executions;
import org.zkoss.zkplus.spring.SpringUtil;
import org.zkoss.zul.Textbox;
import org.zkoss.zul.Window;

public class RegistrationController extends Window{

	private static final long serialVersionUID = 8641382826702741325L;
	private Logger logger = Logger.getLogger(NewClassController.class.getName());
	private UserService userService;
	
	public void setUserService(UserService userService) {
		this.userService = userService;
	}

	public void onRegister(){
		logger.info("registering");
		//FIXME:These should be bind
		Textbox usernameBox = (Textbox)this.getFellow("userName");
		Textbox passwordBox = (Textbox)this.getFellow("password");
		String userName = usernameBox.getValue().trim();
		String password = passwordBox.getValue().trim();
		userService.createUser(new UserForm(userName, password));
		Executions.sendRedirect("frontpage.html");
	}
	/**
	 * This method is automatically called
	 * when the ui page is requested.
	 */
	public void onCreate(){
		logger.info("fetching spring beans");
		try {
			userService = (UserService)SpringUtil.getBean("userService");
		}catch (IllegalStateException ex){
			logger.log(Log.ERROR, "error, could not load spring beans!", ex);
		}
	}
}
